package com.course.coursedesign2022.service;

import com.course.coursedesign2022.mapper.UserMapper;
import com.course.coursedesign2022.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

//每月评级
@Service
public class ExtendDemand {

    @Autowired
    private UserMapper userMapper;

    public String GetLevel(Integer id){
        String level = "C";
        UserInfo userInfo = userMapper.getUserInfoById(id);
        int growScore = userInfo.getGrowscore();
        if(growScore>10&&growScore<=25){
            level="B";
        }else if (growScore>25){
            level="A";
        }
        userInfo.setAssessLevel(level);
        System.out.println("用户"+id+"评级："+level);
        userMapper.updateUserInfo(userInfo);
        return level;
    }

    //可交换积分，到年底重置归零
    public UserInfo UpdateExchangeScore(Integer id){
        UserInfo userInfo = userMapper.getUserInfoById(id);
        Date lastLogin = userInfo.getLastLogin();
        if(lastLogin == null){
            userInfo.setLastLogin(new Date());
            userInfo.setThisLogin(new Date());
        }else {
            Calendar calendar = Calendar.getInstance();
            Calendar calendar1 = Calendar.getInstance();
            calendar.setTime(lastLogin);
            calendar1.setTime(userInfo.getThisLogin());
            int year = calendar.get(Calendar.YEAR);
            int growScore = userInfo.getGrowscore();
            int thisYear = calendar1.get(Calendar.YEAR);
            if (year != thisYear) {
                userInfo.setExchangescore(0);
                userInfo.setScoretotal(growScore);
            }
        }
        System.out.println("用户"+id+"可交换积分已更新");
        userMapper.updateUserInfo(userInfo);
        return userInfo;
    }
}
